﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DocViewer.Common;

public partial class OwnerEdit : System.Web.UI.Page
{
    DY.BLL.tbl_KeyValue bllKeyValue = new DY.BLL.tbl_KeyValue();
    DY.BLL.tbl_Owner bllOwner = new DY.BLL.tbl_Owner();
    DY.BLL.tbl_Protocol bllProtocol = new DY.BLL.tbl_Protocol();
    int id = SiteAbout.gInt("id");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        BindDropDownList("AnAddress", ddl_An_Address);
        BindDropDownList("AnJianFangFangShi", ddl_An_BuildType);
        BindDropDownList("YesNo", ddl_An_GeLou);
        BindDropDownList("AnType", ddl_An_Type);
        BindDropDownList("Strucure", ddl_Chai_Structure);
        BindDropDownList("ProcType", ddl_ProcType);
        BindDropDownList("VillageName", ddl_Village);

        if (SiteAbout.gInt("id") != 0)
        {
            ;
        }
        
    }

    private void BindDropDownList(string keyName, DropDownList ddl)
    {
        ddl.DataSource = bllKeyValue.GetList("KeyName='"+keyName+"'");
        ddl.DataTextField = "KeyDisplay";
        ddl.DataValueField = "KeyValue";
        ddl.DataBind();
        ddl.Items.Insert(0, "请选择...");
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        DY.Model.tbl_Owner modelOwner = new DY.Model.tbl_Owner();
        DY.Model.tbl_Protocol modelProtocol = new DY.Model.tbl_Protocol();
        if (SiteAbout.gInt("id") != 0)
        {
            ;
        }
        modelOwner.CardId = txt_IdCard.Text.Trim();
        //modelOwner.Mobile
        modelOwner.Name = txt_Name.Text.Trim();
        modelOwner.Population = GetTextInt(txt_Population);
        modelOwner.VillageName = ddl_Village.SelectedValue;
        int ownerId = bllOwner.Add(modelOwner);

        modelProtocol.An_Address = ddl_An_Address.SelectedValue;
        //modelProtocol.An_Area = 0;
        modelProtocol.An_Area = GetTextDecimal(txt_An_Area);
        modelProtocol.An_BuildType = ddl_An_BuildType.SelectedValue;
        //modelProtocol.An_ChaiJianArea = 0;
        modelProtocol.An_ChaiJianArea = GetTextDecimal(txt_An_ChaiJianArea);
        modelProtocol.An_ChaiJianNo = txt_An_ChaiJianNo.Text.Trim();
        //modelProtocol.An_Covers = 0;
        modelProtocol.An_Covers = GetTextDecimal(txt_An_Covers);
        modelProtocol.An_GeLou = GetTextInt(ddl_An_GeLou);
        modelProtocol.An_HouseNo = txt_An_HouseNo.Text.Trim();
        modelProtocol.An_HuXing = txt_An_Huxing.Text.Trim();
        modelProtocol.An_TaoShu = GetTextInt(txt_An_Taoshu);
        modelProtocol.An_Time = GetTextDateTime(txt_An_Time);
        modelProtocol.An_Type = ddl_An_Type.SelectedValue;
        modelProtocol.Chai_AttachedArea = GetTextDecimal(txt_Chai_Chai_AttachedArea);
        modelProtocol.Chai_AttachedCovers = GetTextDecimal(txt_Chai_Chai_AttachedCovers);
        modelProtocol.Chai_Fangqianwuhou = GetTextDecimal(txt_Chai_Fangqianwuhou);
        modelProtocol.Chai_MainArea = GetTextDecimal(txt_Chai_MainArea);
        modelProtocol.Chai_MainCovers = GetTextDecimal(txt_Chai_MainCovers);
        modelProtocol.Chai_ReclamationLotNo = txt_Chai_ReclamationLotNo.Text.Trim();
        modelProtocol.Chai_Structure = ddl_Chai_Structure.SelectedValue;
        modelProtocol.Chai_Time = GetTextDateTime(txt_Chai_Time);
        modelProtocol.Fei_AnZhiYanQiBuChang = GetTextDecimal(txt_Fei_AnZhiYanQiBuChang);
        modelProtocol.Fei_ChaiJiaJiangLi = GetTextDecimal(txt_Fei_ChaiJiaJiangLi);
        modelProtocol.Fei_ChaiJianJinE = GetTextDecimal(txt_Fei_ChaiJianJinE);
        modelProtocol.Fei_ChaiJiuFei = 0;
        modelProtocol.Fei_DaiJianShuiNiYingHua = GetTextDecimal(txt_Fei_DaiJianShuiNiYingHua);
        modelProtocol.Fei_DianBiaoAnZhuangFei = GetTextDecimal(txt_Fei_DianBiaoAnZhuangFei);
        modelProtocol.Fei_DiJiBuChaFei = GetTextDecimal(txt_Fei_DiJiBuChaFei);
        modelProtocol.Fei_FangKuanShuE = GetTextDecimal(txt_Fei_Fangkuanshue);
        modelProtocol.Fei_GeLouJinE = GetTextDecimal(txt_Fei_GeLouJinE);
        modelProtocol.Fei_GuoDuQiFangZu = GetTextDecimal(txt_Fei_GuoDuQiFangZu);
        modelProtocol.Fei_LaJiQingYunFei = GetTextDecimal(txt_Fei_LaJiQingYunFei);
        modelProtocol.Fei_ShiGongBaoZhengJin = GetTextDecimal(txt_Fei_ShiGongBaoZhengJin);
        modelProtocol.Fei_ShiGongShuiDianFei = GetTextDecimal(txt_Fei_ShiGongShuiDianFei);
        modelProtocol.Fei_ShiJiaoFeiYong = 0;//decimal.Parse(txt_Fei_YiJiaoShuE.Text.Trim());//TODO
        modelProtocol.Fei_ShuiBiaoKaiHuFei = GetTextDecimal(txt_Fei_ShuiBiaoKaiHuFei);
        modelProtocol.Fei_WeiJiaoShuE = GetTextDecimal(txt_Fei_WeiJiaoShuE);
        modelProtocol.Fei_WeiYueFaKuan = GetTextDecimal(txt_Fei_WeiYueFaKuan);
        modelProtocol.Fei_YiJiaoShuE = GetTextDecimal(txt_Fei_YiJiaoShuE);
        modelProtocol.Fei_YingJiaoShuE = GetTextDecimal(txt_Fei_YingJiaoShuE);
        modelProtocol.Fei_ZhengCeXingBuZhu = GetTextDecimal(txt_Fei_ZhengCeXingBuZhu);
        modelProtocol.OwnerId = ownerId;
        modelProtocol.ProtocolId = txt_ProcNo.Text.Trim();
        modelProtocol.ProtocolType = ddl_ProcType.SelectedValue;
        modelProtocol.Remark = txt_Remark.Text.Trim();
        bllProtocol.Add(modelProtocol);
    }
    private decimal GetTextDecimal(TextBox tx)
    {
        if (string.IsNullOrEmpty(tx.Text))
        {
            return 0;
        }
        else
        {
            try
            {
                return decimal.Parse(tx.Text.Trim());
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
    private int GetTextInt(TextBox tx)
    {
        if (string.IsNullOrEmpty(tx.Text))
        {
            return 0;
        }
        else
        {
            try
            {
                return int.Parse(tx.Text.Trim());
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
    private DateTime GetTextDateTime(TextBox tx)
    {
        if (string.IsNullOrEmpty(tx.Text))
        {
            return DateTime.MinValue;
        }
        else
        {
            try
            {
                return DateTime.Parse(tx.Text.Trim());
            }
            catch (Exception ex)
            {
                return DateTime.MinValue;
            }
        }
    }
    private int GetTextInt(DropDownList ddl)
    {
        if (string.IsNullOrEmpty(ddl.SelectedValue.Trim()))
        {
            return 0;
        }
        else
        {
            try
            {
                return int.Parse(ddl.SelectedValue.Trim());
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}